package microcredit.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import microcredit.model.base.Model;
import microcredit.model.domain.Status;
import microcredit.model.util.ModelUtils;

/**
 * Entity implementation class for Entity: TrainingType
 *
 */
@Entity
public class TrainingType extends Model implements Serializable {

	
	public static final String SHORT_NAME = "shortName";
	public static final String LONG_NAME = "longName";
	public static final String MAXIMUM_MEMBERS = "maximumMembers";
	
	private static final long serialVersionUID = 1L;

	public TrainingType() {
		super();
	}
   
	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE)
	private Long id;
	
	private String shortName;
	private String longName;
	
	/*
	 * Template: 
	 * Topic:
	 * 
	 * Trainer-Name: 
	 * Date: 
	 * Total costs:
	 * 
	 * Contents:
	 */
	private String description;
	
	/** If null unlimited */
	private Long maximumMembers; 
	
	@Enumerated(EnumType.STRING)
	@Column(length=1)
	private Status status;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false)
	private Date updateStamp;
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false)
	private Date insertStamp;
	
	@PrePersist
	public void prePersist() throws ValidationException {
		if (getInsertStamp() == null) {
			setInsertStamp(ModelUtils.createTimestamp());
		}
		setUpdateStamp(ModelUtils.createTimestamp());
		validate();
	}
	
	@PreUpdate
	public void preUpdate() throws ValidationException {
		setUpdateStamp(ModelUtils.createTimestamp());
		validate();
	}
	
	public void validate() throws ValidationException {
		if (nullOrEmpty(getShortName())) {
			throw new ValidationException("Please provide a short name!", TrainingType.SHORT_NAME);
		} else if (nullOrEmpty(getLongName())) {
			throw new ValidationException("Please provide a name!", TrainingType.LONG_NAME);
		}
		if (getMaximumMembers() != null && getMaximumMembers() < 0) {
			throw new ValidationException("The amount of maximum members cannot be negative.", TrainingType.MAXIMUM_MEMBERS);
		}
		
	}
	
	public String getCumulatedName() {
		StringBuilder b = new StringBuilder();
		if (getShortName() != null) {
			b.append(getShortName());
		}
		b.append(" - ");
		if (getLongName() != null) {
			b.append(getLongName());			
		}
		return b.toString();
	}

	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	public String getLongName() {
		return longName;
	}

	public void setLongName(String longName) {
		this.longName = longName;
	}


	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Long getMaximumMembers() {
		return maximumMembers;
	}

	public void setMaximumMembers(Long maximumMembers) {
		this.maximumMembers = maximumMembers;
	}

	public Date getUpdateStamp() {
		return updateStamp;
	}

	public void setUpdateStamp(Date updateStamp) {
		this.updateStamp = updateStamp;
	}

	public Date getInsertStamp() {
		return insertStamp;
	}

	public void setInsertStamp(Date insertStamp) {
		this.insertStamp = insertStamp;
	}

	public Long getId() {
		return id;
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}
	
	@Override
	public String toString() {
		if (shortName == null) {
			return "Training ";
		} else {
			return "Training ["+shortName+"]";
		}
	}
}
